--务繁任迫
local wufanrenpo = fk.CreateSkill {
    name = "mcdh_wufanrenpo",
    tags = {Skill.Switch}
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

wufanrenpo:addEffect('active',{
    target_filter =function (self, player, to_select, selected, selected_cards)
        if player:getSwitchSkillState(wufanrenpo.name) == fk.SwitchYang then
            return to_select ~= player and #selected < #selected_cards
        else
            return  #selected < 3 and to_select:isNude() ~= 0
        end
    end,
    can_use = function(self, player)
        return player:usedSkillTimes(wufanrenpo.name, Player.HistoryPhase) == 0 
    end,
    card_filter = function (self, player, to_select, selected)
        if player:getSwitchSkillState(wufanrenpo.name) == fk.SwitchYang then
            return to_select ~= player and #selected < 3
        else
            return false   
        end
    end,
    feasible = function (self, player, selected, selected_cards, card)
        if player:getSwitchSkillState(wufanrenpo.name) == fk.SwitchYang then
            return #selected == selected_cards
        else
            return #selected > 0 
        end
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        
        if player:getSwitchSkillState(wufanrenpo.name,true) == fk.SwitchYang then
            room:askToYiji(player,{min_num = #effect.cards,max_num = #effect.cards,skill_name =wufanrenpo.name,targets = effect.tos,cards=effect.cards,cancelable = false,single_max=1})
        else
            for _, p in ipairs(effect.tos) do
                if player.dead then break end
                if p:isAlive() and not p:isNude() then
                    local id = room:askForCard(p, 1, 1, true, self.name, false,".", "#wufanrenpo:"..player.id  )[1]
                    room:moveCardTo(Fk:getCardById(id), Card.PlayerHand, player, fk.ReasonGive, self.name, nil, false, p.id)
                end
            end
        end
        local n = player:getHandcardNum() - 3
        if n < 0 then
            player:drawCards(-n, self.name)
        else
            room:askForDiscard(player, n, n, false, self.name, false)
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_wufanrenpo"] = "繁迫",
    [":mcdh_wufanrenpo"] = "『务繁任迫』<br>转换技,出牌阶段限一次，你可以①交给至多三名角色各一张牌;②令至多三名角色各交给你一张牌。然后你将手牌数摸至或弃置至三张。",
    ["#wufanrenpo"] = "务繁任迫:交给 %src 一张牌"
}

return wufanrenpo